// 获取当前选项卡ID
function getCurrentTabId(callback)
{
	chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
	{
		if(callback) callback(tabs.length ? tabs[0].id: null);
	});
}

// 向content-script主动发送消息
function sendMessageToContentScript(message, callback)
{
	getCurrentTabId((tabId) =>
	{
		chrome.tabs.sendMessage(tabId, message, function(response)
		{
			if(callback) callback(response);
		});
	});
}

// popup主动发消息给content-script
$('#tmall-btn').click(() => {
	const tmallName = $('#tmall-name').val();

	if(!tmallName){
		alert('宝贝简称不能为空');
		return false;
	}
	sendMessageToContentScript({
		type: 'tmall',
		data: {
			name: tmallName
		}
	}, (response) => {
		if(response) alert('收到来自content-script的回复：'+response);
	});
});